package org.chen.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.chen.bean.UserReceiptAddressBean;
import org.chen.service.impl.RecAddressServiceImpl;
import org.wxf.bean.AdminInfoBean;
import org.wxf.service.impl.AjaxDemoServiceImpl;
import org.wxf.utils.MyUtils;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.regexp.internal.REUtil;

/**
 * 查询用户收货地址
 */
@WebServlet("/QueryRecAddressMessageAction")
public class RecMsgQueryAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取表单数据
		String usertel =  request.getParameter("usertel");
		
		//设置一个对象
		UserReceiptAddressBean recB = MyUtils.getInstance(UserReceiptAddressBean.class);
		
		//将电话放入对象中
		recB.setUsertel(usertel);
		
		// 进行查询操作
		List<UserReceiptAddressBean> lstRecs = MyUtils.getInstance(RecAddressServiceImpl.class)
				.queryRecAddressMsg(recB);
		System.out.println("查询结果：" + lstRecs);
		// 借助第三方的工具将集合转换为json字符串【alibaba的fastjson插件包】
		// 将查询结果集list转换为json字符串
		String strJson = JSON.toJSONString(lstRecs);
		
		//将收货地址放入session
		request.getSession().setAttribute("recName", lstRecs.get(0).getRecname());
		request.getSession().setAttribute("recTel", lstRecs.get(0).getRectel());
		request.getSession().setAttribute("postCode", lstRecs.get(0).getPostcode());
		request.getSession().setAttribute("recAddress", lstRecs.get(0).getRecaddress());
		request.getSession().setAttribute("addressDetail", lstRecs.get(0).getAddressdetail());
		
		
		// ajax响应
		// 响应客户单的请求
		PrintWriter pw = response.getWriter();
		// 将json字符串发回客户端浏览器
		pw.write(strJson);
		pw.flush();
		pw.close();
	}

}
